package com.backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution07 {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates,target,0);
        return res;
    }

    LinkedList<Integer> tmp = new LinkedList<>();
    int sum = 0;
    public void backTracking(int[] candidates,int target,int index) {
        if (sum > target) return;
        if (sum == target) {
            res.add(new ArrayList<>(tmp));
            return;
        }

        for (int i = index; i < candidates.length; i++) {
            tmp.add(candidates[i]);
            sum += candidates[i];
            backTracking(candidates,target,i);
            sum -= candidates[i];
            tmp.removeLast();
        }
    }
}
